/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights;

import static com.schizoidman.android.citylights.IActions.ACTION_PICK_BUILDCONFIG;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.schizoidman.android.citylights.model.TCProjectProxy;
import com.schizoidman.android.citylights.model.TCServerProxy;

public class ProjectListPage extends ListActivity {

	static final int PICK_BUILDCONFIG_REQUEST = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		final TCServerProxy server = new TCServerProxy(getIntent());

		final ArrayAdapter<TCProjectProxy> adapter = new ArrayAdapter<TCProjectProxy>(ProjectListPage.this,
				R.layout.list_item1, server.getProjects());
		setListAdapter (adapter);

		// Set custom title
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title_no_button);

		new AsyncLoader() {
			
			@Override
			protected void load() throws ClientProtocolException, IOException,
					JSONException {
				server.load();
			}
			
			@Override
			public void onLoadingStarted() {
				findViewById(R.id.progress).setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFinished(int status, String errMessage) {
				switch (status) {
				case LOADING_FAILED:
					Toast.makeText(ProjectListPage.this, errMessage, Toast.LENGTH_LONG).show();
					ProjectListPage.this.finish();
				case LOADING_SUCCEDED:
					adapter.notifyDataSetChanged();
					((TextView)findViewById(R.id.title)).setText(getIntent().getData().getAuthority());
					findViewById(R.id.progress).setVisibility(View.GONE);
				}
			}

		}.asyncLoad();
	}

	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
		if (ACTION_PICK_BUILDCONFIG.equals(getIntent().getAction())) {
			startActivityForResult(((TCProjectProxy)l.getItemAtPosition(position)).getData(), PICK_BUILDCONFIG_REQUEST);
		} else {
			setResult(RESULT_OK, ((TCProjectProxy)l.getItemAtPosition(position)).getData());
			finish();
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PICK_BUILDCONFIG_REQUEST:
			// Passing Intent with the buildType URL back to the caller
			if (resultCode == RESULT_OK) {
				setResult(RESULT_OK, data);
				finish();
			}
		}
	}
}


